const todoData = [
  {
    id: 1,
    content: "123",
    completed: true,
  },
  {
    id: 2,
    content: "234",
    completed: false,
  },
  {
    id: 3,
    content: "345",
    completed: true,
  },
];

((doc) => {
  const oList = doc.querySelector(".todo-list");
  const oInput = doc.querySelector("input");
  const oBtn = doc.querySelector("button");

  const todoEvent = new TodoEvent(todoData, oList);
  function bindEvent() {
    oBtn.addEventListener("click", handleAddBtnClick, false);
    oList.addEventListener("click", handleListClick, false);
  }
  function handleAddBtnClick() {
    const val = oInput.value.trim();
    if (val.length) {
      const ret = todoEvent.addTodo({
        id: Date.now(),
        content: val,
        completed: false,
      });

      if (ret && ret === 1001) {
        alert("列表已存在");
        return;
      }
    }
    oInput.value = "";
  }
  function handleListClick(e) {
    const ev = e || window.event,
      tar = ev.target,
      tagName = tar.tagName;
    const id = parseInt(tar.dataset.id);
    console.log(id);

    if (tagName === "INPUT") {
      todoEvent.toggleComplete(tar, id);
    } else if (tagName === "BUTTON") {
      todoEvent.removeTodo(tar, id);
    }
  }
  bindEvent();
})(document);
